package com.yzf.smsforward;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yzf.pcsmsforward.R;
import com.yzf.smsforward.utils.Utils;

import static com.yzf.smsforward.Constants.TAG;

/**
 * 项目名称：FoldingVerifyCode
 * 类描述：url填写对话框类
 * 创建人：Administrator
 * 创建时间：2019年10月1日
 */
public class UrlDialog extends Dialog {
    private Handler mHandler;
    private Context mContext;
    private String content;
    private EditText urlEt;
    private String url = "";
    private TextView errorMsg;
    private TextView errorMsgTv;

    UrlDialog(@NonNull Context context, Handler handler, String content) {
        super(context);
        this.mContext = context;
        mHandler = handler;
        this.content = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.urll_dialog_layout);
        initView();
    }

    private void initView() {
        this.setCanceledOnTouchOutside(false);
        urlEt = this.findViewById(R.id.dialog_url_et);
        errorMsg = this.findViewById(R.id.textView_errerMsg);
        errorMsgTv = this.findViewById(R.id.dialog_error_msg);
        Button btn = this.findViewById(R.id.dialog_btn);
        String Urlstr = Utils.getUrl(mContext);//手机号码
        Log.d(TAG, "content ============" + content);
        if (TextUtils.isEmpty(content)) {
            errorMsg.setVisibility(View.GONE);
        } else {
            content = content + "\n安卓ID:" + com.yzf.smsforward.CardStackActivity.androidId + "\n设备ID:" + com.yzf.smsforward.CardStackActivity.deviceid;
            errorMsg.setVisibility(View.VISIBLE);
            errorMsg.setText(content);
        }
        if (!TextUtils.isEmpty(Urlstr)) {
            urlEt.setText(Urlstr);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                errorMsg.setText("");
                url = urlEt.getText().toString();
                if (!TextUtils.isEmpty(url)) {
                    if (url.contains("http://") || url.contains("https://")) {
                        Utils.save2SP(mContext, Constants.HTTPS_URL, Constants.HTTPS_URL_KEY, url);
                        UrlDialog.this.dismiss();
                        mHandler.sendEmptyMessage(0);
                    } else {
                        errorMsgTv.setText(R.string.setting_url_error);
                    }
                } else {
                    errorMsgTv.setText(R.string.setting_url_error);
                }
            }
        });
        ImageView close = this.findViewById(R.id.dialog_close_iv);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UrlDialog.this.dismiss();
            }
        });
    }
}
